// --------------------------------------------------------------------------------------------------------------------
// <copyright file="OptionsPage.cs" company="Roman '4ux-nbIx' Novitsky">
//   Copyright (c) Roman '4ux-nbIx' Novitsky 2010
// </copyright>
// <summary>
//   Extends a standard dialog functionality for implementing ToolsOptions pages,
//   with support for the Visual Studio automation model, Windows Forms, and state
//   persistence through the Visual Studio settings mechanism.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace TaskbarButtons
{
  #region Namespace Imports

  using System.ComponentModel;
  using System.Diagnostics.CodeAnalysis;
  using System.Drawing;
  using System.Runtime.InteropServices;
  using System.Windows.Forms;

  using EnvDTE;

  using Microsoft.VisualStudio.Shell;

  #endregion


  /// <summary>
  /// Describes and manipulates the options page.
  /// </summary>
  [Guid(GuidList.GuidPageCustom)]
  public sealed class OptionsPage : DialogPage
  {
    #region Constants and Fields

    private DTE _applicationObject;
    private ButtonsConfiguration _buttonsConfiguration;
    private OptionsPageControl _optionsControl;
    private Package _packageService;

    #endregion


    #region Properties

    /// <summary>
    /// Gets the application object.
    /// </summary>
    /// <value>
    /// The application object.
    /// </value>
    public DTE ApplicationObject
    {
      get
      {
        return _applicationObject ?? (_applicationObject = GetService(typeof(DTE)) as DTE);
      }
    }

    /// <summary>
    /// Gets or sets the buttons configuration.
    /// </summary>
    /// <value>
    /// The buttons configuration.
    /// </value>
    public ButtonsConfiguration ButtonsConfiguration
    {
      get
      {
        return _buttonsConfiguration
               ??
               (_buttonsConfiguration =
                ButtonsConfiguration.Deserialize(PackageService, ApplicationObject)
                ?? ButtonsConfiguration.GetDefaultButtonsList(ApplicationObject));
      }

      set
      {
        _buttonsConfiguration = value;
      }
    }


    /// <summary>
    /// Gets the window an instance of <see cref="DialogPage"/> that it uses as its user interface.
    /// </summary>
    /// <value>
    /// The window.
    /// </value>
    [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", 
      Justification = "Cannot do this.")]
    [Browsable(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    protected override IWin32Window Window
    {
      get
      {
        return _optionsControl
               ?? (_optionsControl = new OptionsPageControl { Location = new Point(0, 0), OptionsPage = this });
      }
    }


    /// <summary>
    /// Gets the package service.
    /// </summary>
    /// <value>
    /// The package service.
    /// </value>
    private Package PackageService
    {
      get
      {
        return _packageService ?? (_packageService = (Package)GetService(typeof(Package)));
      }
    }

    #endregion


    #region Methods

    /// <summary>
    /// Raises the Apply event.
    /// </summary>
    /// <param name="e">
    /// The <see cref="Microsoft.VisualStudio.Shell.DialogPage.PageApplyEventArgs"/> instance containing the event data.
    /// </param>
    protected override void OnApply(PageApplyEventArgs e)
    {
      base.OnApply(e);

      _optionsControl.ApplyChanges();

      ButtonsConfiguration.Serialize(PackageService, Window.Handle);

      if (TaskbarButtonsPackage.Instance != null)
      {
        TaskbarButtonsPackage.Instance.SetButtons(ButtonsConfiguration.TaskbarButtons);
      }
    }

    #endregion
  }
}